<?php
/**
 * Created by PhpStorm.
 * User: DELL
 * Date: 2017/2/8
 * Time: 21:55
 */
namespace frontend\controllers;

use Yii;
use yii\base\InvalidParamException;
use yii\web\BadRequestHttpException;
use yii\web\Controller;
use yii\filters\VerbFilter;
use yii\filters\AccessControl;
use common\models\Goods;
use common\models\Onsales;
use common\models\Goodsclick;

class GoodsController extends Controller
{
    public function actionIndex()
    {
        $id = Yii::$app->request->get('id');
        if ($id && preg_match('/^\d+$/', $id)) {
            $goods_info = Onsales::find()->where(['id' => $id])->asArray()->one();
            if (empty($goods_info)) {
                echo "没有找到商品";exit;
            }
            $arr_goods = Goods::find()->where(['id' => $goods_info['gid']])->asArray()->one();
            $model = new Goodsclick();
            $click = $model->findOne(['goods_id' => $id]);
            if ($click) {
                $click->clicks = $click->clicks + 1;
                $click->update();
            } else {
                $model->goods_id = $id;
                $model->clicks = 1;
                $model->save();
            }

            $this->getView()->title = $arr_goods['goods_name'];
        } else {
            $this->redirect("/?code=index");
            Yii::$app->response->send();
            exit;
        }

        $goods_info['back_url'] = empty(Yii::$app->request->referrer)  || preg_match('/zahuopuzi/', Yii::$app->request->referrer) ? Yii::$app->homeUrl : Yii::$app->request->referrer;
        $goods_info['picture'] = json_decode($goods_info['picture'], true);

        return $this->render('index',['arr_goods' => $arr_goods, 'goods_info' => $goods_info]);
    }
}